package com.kmproject.haifu.mvp.ui.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.utils.DeviceUtils;
import com.kmproject.haifu.R;


import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author:zhangjing
 * Time:2018/6/29  上午11:01
 * Name: this is TabItemView
 * Email:zhangjingworkmol@163.com
 * 自定义itemview
 */

public class TabItemView extends FrameLayout {
    int selectorDrawable;
    int selectorColor;
    int selectDrawable;
    int normalDrawable;
    int selectTextColor;
    int normalTextColor;
    int textSize;
    String text;
    View mView;
    ViewHolder viewHolder;
    //        是否显示红点
    boolean isshowBGAView;

    public TabItemView(@NonNull Context context) {
        super(context);
    }

    private void init() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.item_tabview, this, false);
        ViewHolder viewHolder = new ViewHolder(mView);
        viewHolder.tabicon.setBackgroundResource(selectorDrawable);
        viewHolder.tabtext.setTextSize(textSize == 0 ? 10 : DeviceUtils.dpToPixel(getContext(), textSize));
        if (selectorColor != 0) {
            viewHolder.tabtext.setTextColor(getContext().getResources().getColorStateList(selectorColor));
        }
        viewHolder.tabtext.setText(text);
        if (text==null||TextUtils.isEmpty(text))
        {
            viewHolder.tabtext.setVisibility(View.GONE);
        }
        addView(mView);
    }

    public static class Builder {
        int selectorDrawable;
        int selectorColor;
        int selectDrawable;
        int normalDrawable;
        int selectTextColor;
        int normalTextColor;
        int textSize;
        String text;

        //        是否显示红点
        boolean isshowBGAView;

        public Builder selectorDrawable(int selectorDrawable) {
            this.selectorDrawable = selectorDrawable;
            return this;
        }

        public Builder selectorColor(int selectorColor) {
            this.selectorColor = selectorColor;
            return this;
        }

//        public Builder selectorColor(String text) {
//            this.text = text;
//            return this;
//        }

        public Builder selectDrawable(int selectDrawable) {
            this.selectDrawable = selectDrawable;
            return this;
        }

        public Builder normalDrawable(int normalDrawable) {
            this.normalDrawable = normalDrawable;
            return this;
        }

        public Builder normalTextColor(int normalTextColor) {
            this.normalTextColor = normalTextColor;
            return this;
        }

        public Builder selectTextColor(int selectTextColor) {
            this.selectTextColor = selectTextColor;
            return this;
        }

        public Builder textSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        public Builder text(String text) {
            this.text = text;
            return this;
        }

        public TabItemView build(Context context) {
            TabItemView tabItemView = new TabItemView(context);
            tabItemView.isshowBGAView = this.isshowBGAView;
            tabItemView.normalDrawable = this.normalDrawable;
            tabItemView.selectDrawable = this.selectDrawable;
            tabItemView.selectorColor = this.selectorColor;
            tabItemView.text = this.text;
            tabItemView.selectorDrawable = this.selectorDrawable;
            tabItemView.selectTextColor = this.selectTextColor;
            tabItemView.textSize = this.textSize;
            tabItemView.normalTextColor = this.normalTextColor;
            tabItemView.init();
            return tabItemView;
        }
    }

    static class ViewHolder {
        @BindView(R.id.tabicon)
        ImageView tabicon;
        @BindView(R.id.tabtext)
        TextView tabtext;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        viewHolder = null;
        mView = null;
    }
}
